/**
 * Created by 小润 on 2017/3/27.
 */
$(document).ready(function(){
//  var ajaxData,shareList,feedId,userId;
//  init();
//
////初始化页面
//function init(){
//  $.ajax({
//      url: '/api/v1/person/details',
//      type: "post",
//      async: true,
//      success: function (data) {
//          ajaxData = data;
//          initPage(data);
//          console.log(data.result);
//      },
//      error: function (msg) {
//          //alert('请求失败');
//          console.log(msg)
//      }
//  });
//}
//点赞or取消赞
$('.dianzan').click(isLike);

//创建评论
$('.comment').click(commentEdit);

//查看帖子详情
/*$('#share .container').click(shareDetail);

//帖子详情
function shareDetail(shareList){
    var index = $(this).index();
    feedId = shareList[index].id;
    var data = {
        "feedId": feedId,
        "userId": userId
    };
    $.ajax({
        type: "get",
        url: '/api/v1/feedService/get/one',
        data:data,
        success: function (data) {

        },
        error: function (msg) {
            alert('请求失败');
            console.log(msg)
        }
    });

}*/

//点赞or取消赞
    function isLike(){
        var likeCount = $(this).children('.dianzan-count').text();
        var liked=$(this).children('.liked').val();
        var fid=$(this).children(".fid").val();
        var userId=localStorage.getItem("userId");
      //  alert(userId);
        var obj=$(this)
        var data = {
            "customerId":userId,
            "id": fid,
        };
        if (liked==0){
            $.ajax({
                type: "get",
                url: '/api/v1/feedService/like/create',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan.png');
                  //  alert("点赞成功！");
                    //点赞成功之后把值变成1
                    obj.children(".liked").val(1);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=parseInt(likeCount)+parseInt(1);
                    obj.children('.feelicount').val(likeCount)
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
        else{
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/like/delete',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan1.png');
                  //  alert("取消成功！");
                    obj.children(".liked").val(0);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=likeCount-1;
                    obj.children('.feelicount').val(likeCount)
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
    }


//新增评论
    function commentEdit() {
        var index = $(this).parents("li").index()//点击第几条数据的检索值
        var obj=$(this)
        var disCount = $(this).children('.pinglun-count').text();
        var liked=$(this).children('.liked').val();
        var userId=localStorage.getItem("userId");
        $(".cmt_cont").val("")//初始化
        $(".cmt_btm").animate({
            bottom: "0"
        }, 500)

        // 发表评论
        $("#cmt_issue").off("click").on("click",function () {
            $(".cmt_btm").animate({
                bottom: "-40px"
            },500)
            var toArea = $("#share").children("ul").children("li").eq(index).find(".cmt_area")//添加数据位置
            console.log(toArea)
            var feedId = $(".fid").val()//评论
            var content = $(".cmt_cont").val()//内容
            var data ={
                "userId":userId,
                "content":content,
                "id":feedId
            }
            if(content.trim().length==0 || content==null){
                alert("请输入内容")
                return false;
            }else{
                $.ajax({
                    type:"post",
                    url:"/api/v1/feedService/comment/create",
                    data:data,
                    success:function(data){
                        console.log(data)
                        name=data.data.customer.name
                        obj.children(".pinglun-count").text(" ");
                        disCount=parseInt(disCount)+parseInt(1)
                        obj.children(".pinglun-count").text(disCount);

                        //<input hidden="hidden" class="commentId" value="${li.id}"/>
                        toArea.prepend('<p class="issue_cont" onclick="deleteComment('+data.data.id+',$(this))">' +
                            '<input hidden="hidden" class="commentId" value='+data.data.id+'/><input hidden="hidden" class="userid" value='+userId+'/><span class="issue_id">'+name+'</span><span>：</span><span>'+content+'</span></p>')
                        
                    },
                    error:function(err){
                        //alert("issue failed")
                    }

                })
            }
        })
        return false
    }
//阻止事件冒泡
    $(".cmt_btm").on("click",function(){
        return false
    })

    $("body").on("click",function(){
        $(".cmt_btm").animate({
            bottom:"-40px"
        },500)
    })
});

//历程
$(function(){
    $("#up_Img").on('click',function(){
        $(".popup").animate({
            bottom:"0"
        },1000)
    })

    $(".cancle").on("click",function(){
        $(".popup").animate({
            bottom:"-220px"
        },1000)

    })

    //发布图片
    $(".issue_img").on("click",function(){
        $(".popup").hide()
        $(".process").hide()
        $(".frame_box").show()
        $("#iss").show()
        $(".popup").animate({
            bottom:"-220px"
        },1000)
        $(".frame_box textarea").val("")//清除之前样式
        $("#Img").attr("src","/static/h5/images/photo-load.png")

    })
        var filesArray;
        var headUrl,coverUrl;
        var inputElement = document.getElementById("fileElem");       //家族头像
        inputElement.addEventListener("change", handleFiles, false);
        function handleFiles() {
            filesArray = this.files;
            upload();
        }
        function upload() {
            console.log(filesArray[0]);
            sendFile(filesArray[0])
        }
        function sendFile(file) {
            console.log(typeof file)
            var url = "/api/v1/upload/";    //文件上传接口地址 ?type=3&id=1
            var xhr = new XMLHttpRequest();
            var fd = new FormData();
            xhr.open("post", url, true);
          /*  xhr.setRequestHeader("Content-Type","multipart/form-data;");*/
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    headUrl = xhr.responseText;

                }
            };
            fd.append('file', file);
            xhr.send(fd);
        }
/*     $("#fileElem").on("change", function(){
        var data=$("#infoLogoForm").serialize()
        console.log(data)
        $.ajax({
            url: "/ajax/fileUpload",
            type: 'post',
            data: data,
            dataType:"json",
            success : function(data) {
                console.log(data)
            }
        });
    });*/





    //	发布历程
    $("#iss").on("click",function(){

        if($("#Img").attr("src") == "/static/h5/images/photo-load.png"){
            alert("图片不能为空")
        }else{

            var $year,$day
            var $fName = $(".frame_box textarea").val()
            // $.ajax({
            // 	 type: "post",
            //     url: '/ajax/fileUpload?trpe=3&id=1',
            //     data: {
            //         fName:$fName,
            //         fDesp:$fDesp
            //     },
            //     success: function (data) {
            //
            //         console.log(data.result);

            $(".popup").show()
            $(".process").show()
            $(".frame_box").hide()
            $("#iss").hide()
//
//                  	for (var i=0;i<data.length;i++) {

            $("#img_box").prepend('<ul class="pro_content"><li class="date"><p id="year">2017</p><p id="day">0526</p></li>'
                +'<li class="circle"></li><li class="up_box"><h5 id="explain">'+$fName+'</h5><img  id="rec_Img"/></li></ul>')
            $("#rec_Img").attr("src",Img.src)
//                  	}

            //     },
            //     error: function (msg) {
            //         alert('请求失败');
            //         console.log(msg)
            //
            //     }
            // })

        }

    })
})
//发送分享
$(".share_btn").click(createShare);
//创建分享
function createShare(){
    $("#message_share").hide()
    $("#publish").show()

    $(".cancle_share").on("click",function(){
        $("#message_share").show()
        $("#publish").hide()
    })

    $(".issue_share button").on("click",function(){
        if($(".share_cont").val()==""){
            $(".issue_share button").attr({
                "disabled":false
            })
        }else{
            $("#share ul li").remove()
            $("#message_share").show()
            $("#publish").hide()
            customerId=localStorage.getItem("userId");
            var content = $(".share_cont").val()
            var data = {
                "userId":customerId,
                "tagId":4,//分享
                "content":content,
            }
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/create',//接口地址？
                data: data,
                success: function (data) {
                    console.log('请求成功');
                   /* $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
                        +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
                        +'<div class="col-xs-6 share-content"><img src="${ctx}/static/h5/images/share-img.png" class="share-img" alt=""/>'
                        +'<span class="share-text">'+issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
                        +' <img src="${ctx}/static/h5/images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
                        +'<img src="${ctx}/static/h5/images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')*/
                    window.location.href="/h5/index/home?userId="+customerId;
                },
                error: function (msg) {
                    console.log('请求失败');
                    console.log(msg)
//	                    $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
//                  +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
//                  +'<div class="col-xs-6 share-content"><img src="../../images/share-img.png" class="share-img" alt=""/>'
//                  +'<span class="share-text">'+data.issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
//                  +' <img src="../../images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
//                  +'<img src="../../images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')
                }
            });

        }
    })
}
//分享时获取地理位置
$("#locMess").on("click",function(){
    var pos = new BMap.Geolocation()
    pos.getCurrentPosition(function(suc){
        $("#locMess").html("")
        $("#locMess").html(suc.address.street)
    })
})

function deleteComment(feedId,obj) {

    $(".del_cmt").animate({
        bottom:"0"
    },300)
    // var feedId=$(this).children(".commentId").val();
    var userId=localStorage.getItem("userId");
    var count=obj.parent(".cmt_area").siblings(".share-method").children(".comment").children(".pinglun-count");
    var text=count.text();
    $(".del").on("click",function(){
        $(".del_cmt").animate({
            bottom:"-75px"
        },300)
        // @parm feedid,userId,
        var data={
            "customerId":userId,
            "commentId":feedId
        }
        console.log(data)
        $.ajax({
            type:"post",
            url:"/api/v1/feedService/comment/delete",
            data:data,
            success:function(data){
                obj.remove();
                count.text(" ");
                count.text(parseInt(text)-parseInt(1));
                console.log("请求成功")
            },
            error:function (data) {
                console.log("请求失败")
            }
        });

    })
    $(".cancle").on("click",function () {
        $(".del_cmt").animate({
            bottom:"-75px"
        },300)

    })
    $("body").on("click",function () {
        $(".del_cmt").animate({
            bottom:"-75px"
        },300)
    })
    return false
}

//删除评论
$(".cmt_area").on("click",".issue_cont",function(){
    var userid=$(this).children(".userid").val();
    var userId=window.localStorage.getItem("userId");
    if(userid==userId) {
        var obj = $(this)
        $(".del_cmt").animate({
            bottom: "0"
        }, 300)
        var feedId = $(this).children(".commentId").val();
        var userId = localStorage.getItem("userId");
        var count = $(this).parent(".cmt_area").siblings(".share-method").children(".comment").children(".pinglun-count");
        var text = count.text();
        $(".del").on("click", function () {
            $(".del_cmt").animate({
                bottom: "-75px"
            }, 300)
            // @parm feedid,userId,
            var data = {
                "customerId": userId,
                "commentId": feedId
            }
            $.ajax({
                type: "post",
                url: "/api/v1/feedService/comment/delete",
                data: data,
                success: function (data) {
                    obj.remove();
                    count.text(" ");
                    count.text(parseInt(text) - parseInt(1));
                },
                error: function (data) {

                }
            });

        })
    }
    return false
});
$(".cancle").on("click",function () {
    // $(".del_cmt").animate({
    //     bottom:"-75px"
    // },300)
    return false
})
$("body").on("click",function () {
    $(".del_cmt").animate({
        bottom:"-75px"
    },300)
})

